package com.skytech.component.crawler.web;

import com.alibaba.fastjson.JSONObject;
import com.skytech.component.crawler.scratch.base.PageContentService;

import com.skytech.component.crawler.task.CrawlerTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;


@RestController

public class PageContentController {
    @Autowired
    private PageContentService pageContentService;
    @Autowired
    private CrawlerTask crawlerTask;

    /**
     * 查询
     * @param jsonObject
     * @param response
     * @return
     */
    @PostMapping("crawler/news/content/condition")
    public JSONObject findPageContentByCondition(@RequestBody JSONObject jsonObject, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");

        String newsType = jsonObject.getString("newsType");
        String storage = jsonObject.getString("storage");
        String publish= jsonObject.getString("publish");
        String beginTime= jsonObject.getString("beginTime");
        String endTime= jsonObject.getString("endTime");
        String catalog= jsonObject.getString("catalog");
        String keywords= jsonObject.getString("keywords");
        String abandon= jsonObject.getString("abandon");
        Integer pageSize= jsonObject.getInteger("pageSize");
        Integer pageNo= jsonObject.getInteger("pageNo");
        return pageContentService.findPageContentByCondition(abandon,newsType, storage, publish,beginTime, endTime, catalog, keywords, pageSize, pageNo);
    }

    @PostMapping("crawler/news/content/count")
    public String count(@RequestBody JSONObject jsonObject, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");

        String storage = jsonObject.getString("storage");
        String publish= jsonObject.getString("publish");
        String abandon= jsonObject.getString("abandon");
        String newsType= jsonObject.getString("newsType");

        return pageContentService.count(abandon,storage,publish,newsType);
    }

    /**
     * 发布
     * @param newsId
     * @param response
     * @return
     */
    @GetMapping("crawler/news/content/publish/{newsId}")
    public boolean publish(@PathVariable String newsId,HttpServletResponse response){

        response.setHeader("Access-Control-Allow-Origin","*");

        return pageContentService.publish(newsId);
    }

    /**
     * 废弃
     * @param newsId
     * @param response
     * @return
     */
    @GetMapping("crawler/news/content/abandon/{newsId}")
    public boolean abandon(@PathVariable String newsId,HttpServletResponse response){

        response.setHeader("Access-Control-Allow-Origin","*");

        return pageContentService.abandon(newsId);
    }

    /**
     * 入库
     * @param newsId
     * @param response
     * @return
     */
    @GetMapping("crawler/news/content/storage/{newsId}")
    public boolean storage(@PathVariable String newsId,HttpServletResponse response){

        response.setHeader("Access-Control-Allow-Origin","*");

        return pageContentService.storage(newsId);
    }

    @GetMapping("crawler/news/content/backUp/")
    public void backUpData(HttpServletResponse response){

        response.setHeader("Access-Control-Allow-Origin","*");
        crawlerTask.backUpData();

    }
}
